const app = getApp()
Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    detail: {
      type: Object,
      value: {}
    }
  },
  observers: {},
  data: {
    specKeys: [],
    current: {},
    sliceIndex: 0,
    copyDetail: {},
    userInfo: {}
  },
  methods: {
    beforeOpen () {
      if (this.data.detail.spec.length > 0) {
        this.data.detail.spec.map((item, index) => item.specItem.map((v, num) => {
          if (num === 0) {
            if (v.fileUrl) {
              this.setData({ previewImg: v.fileUrl })
            }
            item.activeId = v.id
            this.data.specKeys.splice(index, 1, v.id)
          }
        }))
        this.data.copyDetail = JSON.parse(JSON.stringify(this.data.detail))
        this.setData({ 'detail.spec': this.data.detail.spec, specKeys: this.data.specKeys, copyDetail: this.data.copyDetail })
        this.changePrice(this.data.detail)
      }
    },
    sample () {
      this.triggerEvent('sample', this.data.current)
    },
    selectSpec ({ currentTarget: { dataset: { row, index } } }) {
      this.data.copyDetail.spec.map(item => item.specItem.map(v => {
        if (row.id === v.id) {
          item.activeId = row.id
          if (v.fileUrl) {
            this.setData({ previewImg: v.fileUrl })
          }
        }
      }))
      this.data.specKeys.splice(index, 1, row.id)
      this.setData({ 'copyDetail.spec': this.data.copyDetail.spec, specKeys: this.data.specKeys })
      if (this.data.specKeys.length === this.data.copyDetail.spec.length) {
        this.key = Array.from(new Set(this.data.specKeys)).sort((a, b) => { return a - b }).join('_')
        let newPrice = this.data.copyDetail.specValue.find(item => item.specKey === this.key)
        let previewImg = ''
        if (this.data.previewImg) previewImg = this.data.previewImg
        else previewImg = this.data.copyDetail.Material.picImg
        this.setData({ current: { ...newPrice, previewImg } })
      }
    },
    changePrice (data) {
      if (this.data.specKeys.length === data.spec.length) {
        this.key = Array.from(new Set(this.data.specKeys)).sort((a, b) => { return a - b }).join('_')
        let newPrice = data.specValue.find(item => item.specKey === this.key)
        let previewImg = ''
        if (this.data.previewImg) previewImg = this.data.previewImg
        else previewImg = this.data.copyDetail.Material.picImg
        this.setData({ current: { ...newPrice, previewImg } })
      }
    },
    toggleShowMore ({ currentTarget: { dataset: { item, index } } }) {
      if (item.sliceIndex > 0 && !item.hasMoreBtn) {
        this.data.copyDetail.spec[index].hasMoreBtn = true
        this.data.copyDetail.spec[index].hasMoreBtn = true
        this.data.copyDetail.spec[index].specItem = this.data.copyDetail.spec[index].specItem.slice(0, item.sliceIndex)
        this.setData({ 'copyDetail.spec': this.data.copyDetail.spec })
      } else {
        this.data.copyDetail.spec[index].hasMoreBtn = false
        this.data.copyDetail.spec[index].hasMoreBtn = false
        this.data.copyDetail.spec[index].specItem = this.data.detail.spec[index].specItem
        this.setData({ 'copyDetail.spec': this.data.copyDetail.spec })
      }
    },
    close () {
      this.triggerEvent('close')
    },
    joinProject () {
      this.triggerEvent('joinProject', this.data.current)
    },
    open () {
      this.data.copyDetail.spec.forEach((row, rowIndex) => {
        let query = this.createSelectorQuery()
        query.selectAll(`.spec-item-${row.id}`).boundingClientRect(rect => {
          let count = 0
          let idx = 0
          rect.forEach((item, index) => {
            if (item.left <= rect[0].left) {
              count++
              if (count === 3) {
                idx = index - 1
                row.sliceIndex = idx
                row.showMore = true
                row.hasMoreBtn = true
                this.data.copyDetail.spec[rowIndex].sliceIndex = idx
                this.data.copyDetail.spec[rowIndex].showMore = true
                this.data.copyDetail.spec[rowIndex].hasMoreBtn = true
              }
            }
          })
          if (idx > 0) {
            this.data.copyDetail.spec[rowIndex].specItem = this.data.copyDetail.spec[rowIndex].specItem.slice(0, row.sliceIndex)
            this.setData({ 'copyDetail.spec': this.data.copyDetail.spec })
          } else {
            this.setData({ 'copyDetail.spec': this.data.detail.spec })
          }
        }).exec()
      })
    }
  },
  attached () {
    const userInfo = app.globalData.userInfo ? app.globalData.userInfo : {}
    this.setData({ userInfo: userInfo })
  }
})